package com.example.my_objectives.model;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class History extends Database {

	private final String	LOG	= History.class.getName();

	private long			_id;
	private String			_date;
	private Objective		_objective;
	private double			_current_value;

	public History(SQLiteDatabase db) {
		super(db);
		set_database(db);
		_objective = new Objective(get_database());
	}

	public History(SQLiteDatabase db, long id, String date, Objective objective, double current_value) {
		super(db);
		set_database(db);
		this._id = id;
		this._date = date;
		this._objective = objective;
		this._current_value = current_value;
	}

	public long getId() {
		return _id;
	}

	public void setId(long id) {
		this._id = id;
	}

	public String getDate() {
		return _date;
	}

	public void setDate(String date) {
		this._date = date;
	}

	public Objective getObjective() {
		return _objective;
	}

	public void setObjective(Objective objective) {
		this._objective = objective;
	}

	public double getCurrent_value() {
		return _current_value;
	}

	public void setCurrent_value(double current_value) {
		this._current_value = current_value;
	}

	@Override
	public Exception createTable() {
		Exception returnValue = null;
		try {
			String query = "CREATE TABLE " + TableName.history.toString()
				+ "( id INTEGER, date TEXT, id_objective INTEGER, current_value REAL, "
				+ "CONSTRAINT 'history_pk' PRIMARY KEY(id), "
				+ "CONSTRAINT 'history_fk_objective' FOREIGN KEY(id_objective) REFERENCES "
				+ TableName.objective.toString() + "(id))";
			Log.d(LOG, query);
			get_database().execSQL(query);
		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}

	@Override
	public Exception add() {
		Exception eReturnValue = null;
		try {
			ContentValues values = new ContentValues();
			if (_id != -1) {
				values.put("id", _id);
			}//if
			values.put("date", _date);
			values.put("id_objective", _objective.getId());
			values.put("current_value", _current_value);

			get_database().insert(TableName.objective.toString(), null, values);
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception update(long old_id) {
		Exception eReturnValue = null;
		try {
			ContentValues values = new ContentValues();
			values.put("id", _id);
			values.put("date", _date);
			values.put("id_objective", _objective.getId());
			values.put("current_value", _current_value);

			get_database().update(TableName.history.toString(), values, "id = ?", new String[] { Long.toString(old_id) });
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception delete() {
		Exception eReturnValue = null;
		try {
			String query = "DELETE FROM " + TableName.history.toString() + " WHERE id = " + _id;
			Log.d(LOG, query);
			get_database().execSQL(query);
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception load(long id) {
		Exception returnValue = null;
		try {
			String query = "SELECT * FROM " + TableName.history.toString() + " WHERE id = " + id;
			Log.d(LOG, query);
			Cursor cursor = get_database().rawQuery(query, null);

			if (cursor.moveToFirst()) {
				_id = id;
				_date = cursor.getString(cursor.getColumnIndex("date"));
				_current_value = cursor.getDouble(cursor.getColumnIndex("current_value"));
				_objective = new Objective(get_database());
				_objective.load(cursor.getLong(cursor.getColumnIndex("id_objective")));
			}// if
			cursor.close();
		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}

	/**
	 * All history
	 *
	 * @return All records in the table
	 */
	public ArrayList<ContentValues> getAll() {

		ArrayList<ContentValues> list = new ArrayList<ContentValues>();

		try {
			String query = "SELECT * FROM " + TableName.history.toString();
			Log.d(LOG, query);
			Cursor cursor = get_database().rawQuery(query, null);
			if (cursor.moveToFirst()) {
				while (!cursor.isAfterLast()) {
					ContentValues record = new ContentValues();
					record.put("id", cursor.getString(cursor.getColumnIndex("id")));
					record.put("date", cursor.getString(cursor.getColumnIndex("date")));
					record.put("id_objective", cursor.getString(cursor.getColumnIndex("id_objective")));
					record.put("current_value", cursor.getString(cursor.getColumnIndex("current_value")));

					list.add(record);
				}// while
			}// if
			cursor.close();
		}//try
		catch (Exception e) {
			Log.e(LOG, e.getMessage());
		}//catch
		return list;
	}

}
